
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\indivm.dta", clear
/**************************************/
/* Définition des zones géographiques */
/**************************************/
/**************************************/
/* 1) => Merge avec les ZE 			  */
/**************************************/
/* elimine la corse */
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* définit les ZE */
joinby cgeo using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\cgeo_ze.dta", unm(m)
tab _merge
drop if _merge==1
drop zen

/* foreign born workers */
gen fborn=(depnai=="99")
gen ifborn="N" if fborn==0
replace ifborn="FB" if fborn==1
/* blue collar dummy */
gen ouv=(cs2h=="62" | cs2h=="63" | cs2h=="64" | cs2h=="65" | cs2h=="67" | cs2h=="68" | cs2h=="69" )
gen bc=(ouv==1)

/* Managers */
gen cad=(cs2h=="23" | cs2h=="31" | cs2h=="33" | cs2h=="34" | cs2h=="35" | cs2h=="37" | cs2h=="38")
/* blue collar dummy */

/****************************************/
/* ouvriers qualitifes et non qualifies */
/****************************************/
gen qual=(cs2_anc=="60" | cs2_anc=="61" ///
| cs2=="62" | cs2=="63"| cs2=="64"| cs2=="65")
/* low skilled BLUE COLLARD */
gen bcnq=bc*(1-qual)

/* Technicians */
gen tec=(cs2h=="42" | cs2h=="43" | cs2h=="44" | cs2h=="45" | cs2h=="46" | cs2h=="47" )

/* emp2 => all the rest */
gen emp2=(bc==0 & cad==0 & tec==0)

/* merge technicians & clerks */
gen tecemp2=(tec==1 | emp2==1)

/* share of workers in Construction, Tradable and non-Tradable sector */
/* construction */
gen sconst=(nap40=="24")
/* tradable */
gen strad=((nap40=="01" | nap40=="02" | ///
nap40=="03" | nap40=="04" | nap40=="05" | nap40=="06" | ///
nap40=="07" | nap40=="08" | nap40=="09" | nap40=="10" | ///
nap40=="11" | nap40=="12" | nap40=="13" | nap40=="14" | ///
nap40=="15" | nap40=="16" | nap40=="17" | nap40=="18" | ///
nap40=="19" | nap40=="20" | nap40=="21" | nap40=="22" | ///
nap40=="23" | nap40=="36" | nap40=="37" | nap40=="35" | nap40=="32"))
/* non-tradable */
gen sntrad=((nap40=="24" | nap40=="25" | ///
nap40=="26" | nap40=="27" | nap40=="28" | nap40=="29" | ///
nap40=="30" | nap40=="31" | nap40=="33" | nap40=="34"))

gen cadN=cad*(1-fborn)
gen tecemp2N=tecemp2*(1-fborn)
gen bcN=bc*(1-fborn)
gen bcnqN=bcnq*(1-fborn)

save temp.dta, replace

/* share workers in tradable, non-tradable and construction sector */
use temp.dta, clear
collapse (mean) sconst strad sntrad , by(ze an) fast
save sect.dta, replace

/* number of employees per commuting zone */
use temp.dta, clear
gen emp=1
gen empN=1-fborn
gen lsnd=ln(sn/dp)
collapse (count)  n_emp=emp (sum) n_empN=empN n_cad=cad n_tecemp2=tecemp2 n_bc=bc n_bcnq=bcnq ///
n_cadN=cadN n_tecemp2N=tecemp2N n_bcN=bcN n_bcnqN=bcnqN ///
(mean) mlsnd=lsnd , by(ze an) fast
/* adjust sampling rates */
foreach nvar in emp cad tecemp2 bc bcnq empN cadN tecemp2N bcN bcnqN {
replace n_`nvar'=n_`nvar'*24 if an!=2007
replace n_`nvar'=n_`nvar'*12 if an==2007
}
save nemp.dta, replace

/* Shift-share importé */
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\pit\shiftshare2017.dta", clear
/* immigrants employed per commuting zone */
joinby ze an using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\pit\RED_zepopemp2017.dta", unm(m)

drop _merge

/* adjust census year to DADS years */
replace an=1991 if an==1990
replace an=1976 if an==1975
joinby ze an using nemp.dta, unm(m)
drop if an==2012
drop _merge
joinby ze an using sect.dta, unm(m)
drop _merge

rm nemp.dta
rm sect.dta

/* define panel */
egen ize=group(ze)
egen time=group(an)
xtset ize time

replace n_emp=F.n_emp if an==1968
/* variable d'intérêt: changement du nombre d'employés immigrés low skill */
gen dimm=D.imemp/(L.n_emp)
gen zeimmig=immig/zepop

/* variable d'intérêt revisé 2021: changement du nombre d'employés immigrés low skill / natives in t (instead of t-1) */
gen dimmR21=D.imemp/(n_emp)

/* instruments */
gen dimm68=pit68/L.n_emp
gen dimm75=pit75/L.n_emp
gen dimm82=pit82/L.n_emp
gen dimm90=pit90/L.n_emp
gen dimm99=pit99/L.n_emp

/* instrument revisé 2021: changement PREDIT du nombre d'employés immigrés low skill / natives in t (instead of t-1) */
gen dimm68R21=pit68/n_emp
gen dimm75R21=pit75/n_emp
gen dimm82R21=pit82/n_emp
gen dimm90R21=pit90/n_emp
gen dimm99R21=pit99/n_emp

gen ldimm=L.dimm
gen ldimm68=L.dimm68
gen ldimm75=L.dimm75
gen ldimm82=L.dimm82
gen ldimm90=L.dimm90
gen ldimm99=L.dimm99

/* local growth in the number of employees */
/* adjust sampling rates */
foreach nvar in emp cad tecemp2 bc bcnq{
/* all employees */
gen dl`nvar'R21=ln(n_`nvar')-ln(L.n_`nvar')
/* only natives */
gen dl`nvar'NR21=ln(n_`nvar')-ln(L.n_`nvar'N)
}

/* replace ldimm=D.imemp/(F.n_emp) if missing(ldimm) */

reg dimm82 ldimm82
predict pdimm82, residuals

/* instrument baseline t-2 */
gen dimtm2=dimm68 if an==1975
replace dimtm2=dimm68 if an==1982
replace dimtm2=dimm75 if an==1991
replace dimtm2=dimm82 if an==1999
replace dimtm2=dimm90 if an==2007

gen ldimtm2=ldimm68 if an==1975
replace ldimtm2=ldimm68 if an==1982
replace ldimtm2=ldimm75 if an==1991
replace ldimtm2=ldimm82 if an==1999
replace ldimtm2=L.dimm90 if an==2007

gen lsconst=L.sconst
gen lstrad=L.strad
gen lsntrad=L.sntrad
gen lzepop=ln(L.zepop)
gen dlzepopR21=ln(zepop)-ln(L.zepop)

keep an ze dimm dimmR21 dimm68 dimm75 dimm68R21 dimm75R21 ///
ldimm ldimm68 ldimm75 ldimtm2 dimtm2 zepop ///
lsconst lstrad lsntrad zeimmig zepop lzepop mlsnd dlempR21 dlzepopR21 dlcadR21 dltecemp2R21 dlbcR21 dlbcnqR21 ///
dlempNR21 dlcadNR21 dltecemp2NR21 dlbcNR21 dlbcnqNR21

save "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\pit\instru2017R21.dta", replace
rm temp.dta
